package com.hooper.user.controller.api;

import com.hooper.hosp.model.user.UserInfo;
import com.hooper.hosp.vo.user.LoginVo;
import com.hooper.hosp.vo.user.UserAuthVo;
import com.hooper.hospital.common.result.Result;
import com.hooper.hospital.common.utils.AuthContextHolder;
import com.hooper.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/10/03/14:44
 */
@Api("user control center")
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApiController {

    private final UserService userService;


    @ApiOperation("user login by phone, if the user login for the first time, it will consider it as register and login")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody LoginVo loginVo) {
        Map<String, Object> loginInfo = userService.login(loginVo);
        return Result.ok(loginInfo);
    }

    @GetMapping("auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        return Result.ok(userService.getById(userId));
    }

    @PostMapping("auth/userAuth")
    public Result getUserAuth(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request) {
        userService.getUserAuth(userAuthVo, request);
        return Result.ok();
    }

}
